package com.yuerenjf.yuerenjinfu.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.yuerenjf.yuerenjinfu.R;
import com.yuerenjf.yuerenjinfu.activity.WebActivity;
import com.yuerenjf.yuerenjinfu.adapter.AdapterLendOtherRecycler;
import com.yuerenjf.yuerenjinfu.bean.LendItemBean;
import com.yuerenjf.yuerenjinfu.bean.LendYouXuanBean;
import com.yuerenjf.yuerenjinfu.customview.MyPtrClassicFrameLayout;
import com.yuerenjf.yuerenjinfu.http.Constants;
import com.yuerenjf.yuerenjinfu.http.Xutils;
import com.yuerenjf.yuerenjinfu.listener.OnRecyclerViewItemClickListener;
import com.yuerenjf.yuerenjinfu.utils.RecyclerSpace;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kiddo on 16-9-20.
 */

public class FragmentLendOther extends BaseFragment {
    private final static String ORDER_DEFAULT = "0";
    private final static String ORDER_SHOUYILV_UP = "1";
    private final static String ORDER_SHOUYILV_DOWN = "2";
    private final static String ORDER_QIXIAN_UP = "3";
    private final static String ORDER_QIXIAN_DOWN = "4";

    private TextView mOrderDefaultTv;
    private TextView mOrderNianhuaLvTv;
    private TextView mOrderQiXianTv;
    Handler handler = new Handler();
    //    private PtrFrameLayout ptrFrameLayout;
    MyPtrClassicFrameLayout ptrClassicFrameLayout;
    private RecyclerView mRecyclerView;
    AdapterLendOtherRecycler mAdapter;
    private RecyclerAdapterWithHF mAdapterWithHF;
    int page = 1;
    ArrayList<LendItemBean> beans;
    private String orderType;
    private String type;
    public static FragmentLendOther getInstance(String type) {
        FragmentLendOther fragment = new FragmentLendOther();
        Bundle bundle = new Bundle();
        bundle.putString("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Override
    public void prepareData() {
        type=getArguments().getString("type");
    }

    @Override
    public View prepareView(LayoutInflater inflater, ViewGroup container) {
        View view = inflater.inflate(R.layout.fragment_lend_list, null);

        return view;
    }

    @Override
    public void onloadData(View view) {
        mOrderDefaultTv = (TextView) view.findViewById(R.id.lend_list_order_default_tv);
        mOrderNianhuaLvTv = (TextView) view.findViewById(R.id.lend_list_order_shouyilv_tv);
        mOrderQiXianTv = (TextView) view.findViewById(R.id.lend_list_order_qixian_tv);


        mOrderDefaultTv.setOnClickListener(listener);
        mOrderNianhuaLvTv.setOnClickListener(listener);
        mOrderQiXianTv.setOnClickListener(listener);
        ptrClassicFrameLayout = (MyPtrClassicFrameLayout) view.findViewById(R.id.recycler_view_frame);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        mRecyclerView.addItemDecoration(new RecyclerSpace(10, getResources().getColor(R.color.bg_background_gray)));


        ptrClassicFrameLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                ptrClassicFrameLayout.autoRefresh(true);
            }
        }, 150);


        ptrClassicFrameLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                ptrClassicFrameLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page=1;
                        getDataForRemote(type,orderType,1);

                    }
                }, 200);
            }
        });

        ptrClassicFrameLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                ptrClassicFrameLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = page + 1;
                        Log.i("index",page+"===");
                        getDataForRemote(type,orderType,page);
                    }
                }, 1000);
            }
        });


    }

    private void setListViewData(final List<LendYouXuanBean.DataBean.CurBorrowsBean> curBorrowsBeen, int index) {
        if (mAdapter == null) {
            mAdapter = new AdapterLendOtherRecycler(getActivity());
            mAdapterWithHF = new RecyclerAdapterWithHF(mAdapter);
            mRecyclerView.setAdapter(mAdapterWithHF);
            mAdapter.setOnRecyclerViewItemClickListener(new OnRecyclerViewItemClickListener() {
                @Override
                public void onRecyclerViewItemClick(View view, int position) {
                    WebActivity.launchActivity(getActivity(),curBorrowsBeen.get(position).getBorrow_url());
                }
            });
        }
        if (index == 1) {
            mAdapter.removeDatas();
        }
        mAdapter.addDatas(curBorrowsBeen);
    }


    @Override
    public void adapterScreen() {

    }

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Drawable drawableDefault = getResources().getDrawable(R.drawable.icon_order_default);
            Drawable drawableUp = getResources().getDrawable(R.drawable.icon_order_up);
            Drawable drawableDown = getResources().getDrawable(R.drawable.icon_order_down);
            drawableDefault.setBounds(0, 0, drawableDefault.getMinimumWidth(), drawableDefault.getMinimumHeight()); //设置边界
            drawableUp.setBounds(0, 0, drawableUp.getMinimumWidth(), drawableUp.getMinimumHeight()); //设置边界
            drawableDown.setBounds(0, 0, drawableDown.getMinimumWidth(), drawableDown.getMinimumHeight()); //设置边界
            switch (view.getId()) {
                case R.id.lend_list_order_default_tv:
                    orderType = ORDER_DEFAULT;
                    mOrderDefaultTv.setTextColor(getResources().getColor(R.color.app_theme_color));
                    mOrderQiXianTv.setTextColor(getResources().getColor(R.color.gray_dark));
                    mOrderNianhuaLvTv.setTextColor(getResources().getColor(R.color.gray_dark));
                    mOrderQiXianTv.setCompoundDrawables(null, null, drawableDefault, null);//画在右边
                    mOrderNianhuaLvTv.setCompoundDrawables(null, null, drawableDefault, null);
                    break;
                case R.id.lend_list_order_shouyilv_tv:
                    mOrderQiXianTv.setCompoundDrawables(null, null, drawableDefault, null);//画在右边
                    mOrderDefaultTv.setTextColor(getResources().getColor(R.color.gray_dark));
                    mOrderQiXianTv.setTextColor(getResources().getColor(R.color.gray_dark));
                    mOrderNianhuaLvTv.setTextColor(getResources().getColor(R.color.app_theme_color));
                    if (orderType == ORDER_SHOUYILV_UP) {
                        orderType = ORDER_SHOUYILV_DOWN;
                        mOrderNianhuaLvTv.setCompoundDrawables(null, null, drawableDown, null);
                    } else {
                        orderType = ORDER_SHOUYILV_UP;
                        mOrderNianhuaLvTv.setCompoundDrawables(null, null, drawableUp, null);
                    }
                    break;
                case R.id.lend_list_order_qixian_tv:
                    mOrderNianhuaLvTv.setCompoundDrawables(null, null, drawableDefault, null);
                    mOrderDefaultTv.setTextColor(getResources().getColor(R.color.gray_dark));
                    mOrderNianhuaLvTv.setTextColor(getResources().getColor(R.color.gray_dark));
                    mOrderQiXianTv.setTextColor(getResources().getColor(R.color.app_theme_color));
                    if (orderType == ORDER_QIXIAN_UP) {
                        orderType = ORDER_QIXIAN_DOWN;
                        mOrderQiXianTv.setCompoundDrawables(null, null, drawableDown, null);//画在右边
                    } else {
                        orderType = ORDER_QIXIAN_UP;
                        mOrderQiXianTv.setCompoundDrawables(null, null, drawableUp, null);
                    }

                    break;

            }
            ptrClassicFrameLayout.postDelayed(new Runnable() {

                @Override
                public void run() {
                    mRecyclerView.smoothScrollToPosition(0);
                    ptrClassicFrameLayout.autoRefresh(true);
                }
            }, 150);
        }
    };


    private void getDataForRemote(String type,String orderType,final int page) {
        Map<String, String> map = new HashMap<>();
        map.put("type",type);
        map.put("sort_by",orderType);
        map.put("page",page+"");
        Xutils.getInstance().get(getActivity(), Constants.URL_LIST, map, new Xutils.XCallBack() {
            @Override
            public void onResponse(String result) {
                ptrClassicFrameLayout.refreshComplete();

                LendYouXuanBean mLendYouXuanBean = gson.fromJson(result, LendYouXuanBean.class);
                if (mLendYouXuanBean.getStatus() != 200) {
                    return;
                }
                LendYouXuanBean.DataBean mDataBean = mLendYouXuanBean.getData();
                setListViewData(mDataBean.getCur_borrows(), page);
                LendYouXuanBean.DataBean.PagesBean bean = mDataBean.getPages();
                Log.i("index",bean.getPage_index()+"--"+bean.getPage_total());
                if (bean.getPage_index() == bean.getPage_total()) {
                    mAdapterWithHF.notifyDataSetChanged();
                    ptrClassicFrameLayout.setLoadMoreEnable(false);
                } else {
                    mAdapterWithHF.notifyDataSetChanged();
                    ptrClassicFrameLayout.setLoadMoreEnable(true);
                    ptrClassicFrameLayout.loadMoreComplete(true);
                }


            }

            @Override
            public void onFailer(int status, String result) {

            }

            @Override
            public void onFinish() {
                ptrClassicFrameLayout.refreshComplete();
            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != mAdapter) {
            mAdapter.startRefreshTime();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (null != mAdapter) {
            mAdapter.cancelRefreshTime();
        }
    }
}
